library(Seurat)
load("GCMat.RData")
dims.use <- 1:15
resol <- 0.5

#create
project.Seurat <- CreateSeuratObject(GCMat, project = "P")

#VlnPlot
project.Seurat[["percent.mt"]] <- PercentageFeatureSet(project.Seurat, pattern = "^MT-")
png("vln.png", width = 800, height = 500)
VlnPlot(project.Seurat, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)
dev.off()

#normal
project.Seurat <- Seurat::NormalizeData(object = project.Seurat, normalization.method = "LogNormalize", scale.factor = 10000)

#MT Gene
mito.genes <- grep(pattern = "^MT-", x = rownames(x = project.Seurat@assays[["RNA"]]@counts), value = TRUE)
percent.mito <- Matrix::colSums(project.Seurat@assays[["RNA"]]@counts[mito.genes,]) / Matrix::colSums(project.Seurat@assays[["RNA"]]@counts)
project.Seurat <- Seurat::AddMetaData(object = project.Seurat, metadata = percent.mito, col.name = "percent.mito")

project.Seurat <- Seurat::ScaleData(object = project.Seurat)

#variable Feature
project.Seurat <- FindVariableFeatures(object = project.Seurat,
                                       selection.method = 'mean.var.plot',
                                       mean.cutoff = c(0.1, Inf),
                                       dispersion.cutoff = c(0.5, Inf)
)

#PCA
project.Seurat <- RunPCA(object = project.Seurat,
                         features = VariableFeatures(object = project.Seurat),
                         verbose = FALSE,
                         npcs = 50)

#cluster
project.Seurat <- FindNeighbors(object = project.Seurat, dims = dims.use)
project.Seurat <- FindClusters(object = project.Seurat, resolution = resol)

#tsne
project.Seurat <- RunTSNE(object = project.Seurat, dims = dims.use, check_duplicates = FALSE)
png("tsne.png", width = 800, height = 500)
TSNEPlot(object = project.Seurat, label = TRUE, label.size = 6)
dev.off()

png("feature.png", width = 800, height = 500)
FeaturePlot(project.Seurat, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP", "CD8A"))
dev.off()







